<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>从已连接的socket接收数据</title>
 </head>
 <body class="docs"><div id="layout">
  <div id="layout-content"><div id="function.socket-recv" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">socket_recv</h1>
  <p class="verinfo">(PHP 4 &gt;= 4.1.0, PHP 5, PHP 7)</p><p class="refpurpose"><span class="refname">socket_recv</span> &mdash; <span class="dc-title">从已连接的socket接收数据</span></p>

 </div>
 
 <div class="refsect1 description" id="refsect1-function.socket-recv-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>socket_recv</strong></span>
    ( <span class="methodparam"><span class="type">resource</span> <code class="parameter">$socket</code></span>
   , <span class="methodparam"><span class="type">string</span> <code class="parameter reference">&$buf</code></span>
   , <span class="methodparam"><span class="type">int</span> <code class="parameter">$len</code></span>
   , <span class="methodparam"><span class="type">int</span> <code class="parameter">$flags</code></span>
   ) : <span class="type">int</span></div>

  <p class="para rdfs-comment">
   函数 <span class="function"><strong>socket_recv()</strong></span> 从 <code class="parameter">socket</code> 中接受长度为
   <code class="parameter">len</code> 字节的数据，并保存在 <code class="parameter">buf</code> 中。
   <span class="function"><strong>socket_recv()</strong></span> 用于从已连接的socket中接收数据。除此之外，可以设定一个或多个 flags 
   来控制函数的具体行为。
  </p>
  <p class="para">
   <code class="parameter">buf</code> 以引用形式传递，因此必须是一个以声明的有效的变量。从
   <code class="parameter">socket</code> 中接收到的数据将会保存在 <code class="parameter">buf</code> 中。
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.socket-recv-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>

    
     <dt>
<code class="parameter">socket</code></dt>

     <dd>

      <p class="para">
       参数 <code class="parameter">socket</code> 必须是一个由 <span class="function"><a href="socket_create.html" class="function">socket_create()</a></span> 
       创建的socket资源。
      </p>
     </dd>

    

    
     <dt>
<code class="parameter">buf</code></dt>

     <dd>

      <p class="para">
       从socket中获取的数据将被保存在由 <code class="parameter">buf</code> 制定的变量中。
       如果有错误发生，如链接被重置，数据不可用等等， <code class="parameter">buf</code> 将被设为 <strong><code>NULL</code></strong>。
      </p>
     </dd>

    

    
     <dt>
<code class="parameter">len</code></dt>

     <dd>

      <p class="para">
       长度最多为 <code class="parameter">len</code> 字节的数据将被接收。
      </p>
     </dd>

    

    
     <dt>
<code class="parameter">flags</code></dt>

     <dd>

      <p class="para">
       <code class="parameter">flags</code> 的值可以为下列任意flag的组合。使用按位或运算符(<em>|</em>)来
       组合不同的flag。
      </p>
      
      <table class="doctable table">
       <caption><strong>可用的 <code class="parameter">flags</code> 值</strong></caption>
       
        <thead>
         <tr>
          <th>Flag</th>
          <th>描述</th>
         </tr>

        </thead>

        <tbody class="tbody">
         <tr>
          <td><strong><code>MSG_OOB</code></strong></td>
          <td>
           处理超出边界的数据
          </td>
         </tr>

         <tr>
          <td><strong><code>MSG_PEEK</code></strong></td>
          <td>
           从接受队列的起始位置接收数据，但不将他们从接受队列中移除。
          </td>
         </tr>

         <tr>
          <td><strong><code>MSG_WAITALL</code></strong></td>
          <td>
           在接收到至少 <code class="parameter">len</code> 字节的数据之前，造成一个阻塞，并暂停脚本运行（block）。但是，
           如果接收到中断信号，或远程服务器断开连接，该函数将返回少于 <code class="parameter">len</code> 字节的数据。
          </td>
         </tr>

         <tr>
          <td><strong><code>MSG_DONTWAIT</code></strong></td>
          <td>
           如果制定了该flag，函数将不会造成阻塞，即使在全局设置中指定了阻塞设置。
          </td>
         </tr>

        </tbody>
       
      </table>

     </dd>

        
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.socket-recv-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   <span class="function"><strong>socket_recv()</strong></span> 返回一个数字，表示接收到的字节数。如果发生了错误，则返回 <strong><code>FALSE</code></strong>
   错误码可使用 <span class="function"><a href="socket_last_error.html" class="function">socket_last_error()</a></span> 接收。也可使用函数 <span class="function"><a href="socket_strerror.html" class="function">socket_strerror()</a></span> 
   来取得关于错误的文字描述。
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.socket-recv-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="snmp.set.example.multiple">
    <p><strong>Example #1 <span class="function"><strong>socket_recv()</strong></span> 范例</strong></p>
    <div class="example-contents"><p>
     该范例简单地使用 <span class="function"><strong>socket_recv()</strong></span> 重写了 <a href="sockets.examples.html" class="xref">范例</a> 中的
     第一个例子。
    </p></div>
    <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />error_reporting</span><span style="color: #007700">(</span><span style="color: #0000BB">E_ALL</span><span style="color: #007700">);<br /><br />echo&nbsp;</span><span style="color: #DD0000">"&lt;h2&gt;TCP/IP&nbsp;Connection&lt;/h2&gt;\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">/*&nbsp;Get&nbsp;the&nbsp;port&nbsp;for&nbsp;the&nbsp;WWW&nbsp;service.&nbsp;*/<br /></span><span style="color: #0000BB">$service_port&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">getservbyname</span><span style="color: #007700">(</span><span style="color: #DD0000">'www'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'tcp'</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">/*&nbsp;Get&nbsp;the&nbsp;IP&nbsp;address&nbsp;for&nbsp;the&nbsp;target&nbsp;host.&nbsp;*/<br /></span><span style="color: #0000BB">$address&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">gethostbyname</span><span style="color: #007700">(</span><span style="color: #DD0000">'www.example.com'</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">/*&nbsp;Create&nbsp;a&nbsp;TCP/IP&nbsp;socket.&nbsp;*/<br /></span><span style="color: #0000BB">$socket&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">socket_create</span><span style="color: #007700">(</span><span style="color: #0000BB">AF_INET</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">SOCK_STREAM</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">SOL_TCP</span><span style="color: #007700">);<br />if&nbsp;(</span><span style="color: #0000BB">$socket&nbsp;</span><span style="color: #007700">===&nbsp;</span><span style="color: #0000BB">false</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"socket_create()&nbsp;failed:&nbsp;reason:&nbsp;"&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">socket_strerror</span><span style="color: #007700">(</span><span style="color: #0000BB">socket_last_error</span><span style="color: #007700">())&nbsp;.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />}&nbsp;else&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"OK.\n"</span><span style="color: #007700">;<br />}<br /><br />echo&nbsp;</span><span style="color: #DD0000">"Attempting&nbsp;to&nbsp;connect&nbsp;to&nbsp;'</span><span style="color: #0000BB">$address</span><span style="color: #DD0000">'&nbsp;on&nbsp;port&nbsp;'</span><span style="color: #0000BB">$service_port</span><span style="color: #DD0000">'..."</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$result&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">socket_connect</span><span style="color: #007700">(</span><span style="color: #0000BB">$socket</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$address</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$service_port</span><span style="color: #007700">);<br />if&nbsp;(</span><span style="color: #0000BB">$result&nbsp;</span><span style="color: #007700">===&nbsp;</span><span style="color: #0000BB">false</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"socket_connect()&nbsp;failed.\nReason:&nbsp;(</span><span style="color: #0000BB">$result</span><span style="color: #DD0000">)&nbsp;"&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">socket_strerror</span><span style="color: #007700">(</span><span style="color: #0000BB">socket_last_error</span><span style="color: #007700">(</span><span style="color: #0000BB">$socket</span><span style="color: #007700">))&nbsp;.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />}&nbsp;else&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"OK.\n"</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">$in&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"HEAD&nbsp;/&nbsp;HTTP/1.1\r\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$in&nbsp;</span><span style="color: #007700">.=&nbsp;</span><span style="color: #DD0000">"Host:&nbsp;www.example.com\r\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$in&nbsp;</span><span style="color: #007700">.=&nbsp;</span><span style="color: #DD0000">"Connection:&nbsp;Close\r\n\r\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$out&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">''</span><span style="color: #007700">;<br /><br />echo&nbsp;</span><span style="color: #DD0000">"Sending&nbsp;HTTP&nbsp;HEAD&nbsp;request..."</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">socket_write</span><span style="color: #007700">(</span><span style="color: #0000BB">$socket</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$in</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">strlen</span><span style="color: #007700">(</span><span style="color: #0000BB">$in</span><span style="color: #007700">));<br />echo&nbsp;</span><span style="color: #DD0000">"OK.\n"</span><span style="color: #007700">;<br /><br />echo&nbsp;</span><span style="color: #DD0000">"Reading&nbsp;response:\n\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$buf&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'This&nbsp;is&nbsp;my&nbsp;buffer.'</span><span style="color: #007700">;<br />if&nbsp;(</span><span style="color: #0000BB">false&nbsp;</span><span style="color: #007700">!==&nbsp;(</span><span style="color: #0000BB">$bytes&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">socket_recv</span><span style="color: #007700">(</span><span style="color: #0000BB">$socket</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$buf</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">2048</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">MSG_WAITALL</span><span style="color: #007700">)))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"Read&nbsp;</span><span style="color: #0000BB">$bytes</span><span style="color: #DD0000">&nbsp;bytes&nbsp;from&nbsp;socket_recv().&nbsp;Closing&nbsp;socket..."</span><span style="color: #007700">;<br />}&nbsp;else&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"socket_recv()&nbsp;failed;&nbsp;reason:&nbsp;"&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">socket_strerror</span><span style="color: #007700">(</span><span style="color: #0000BB">socket_last_error</span><span style="color: #007700">(</span><span style="color: #0000BB">$socket</span><span style="color: #007700">))&nbsp;.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">socket_close</span><span style="color: #007700">(</span><span style="color: #0000BB">$socket</span><span style="color: #007700">);<br /><br />echo&nbsp;</span><span style="color: #0000BB">$buf&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />echo&nbsp;</span><span style="color: #DD0000">"OK.\n\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
    </div>

    <div class="example-contents"><p>
     The above example will produce something like:
    </p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
&lt;h2&gt;TCP/IP Connection&lt;/h2&gt;
OK.
Attempting to connect to &#039;208.77.188.166&#039; on port &#039;80&#039;...OK.
Sending HTTP HEAD request...OK.
Reading response:

Read 123 bytes from socket_recv(). Closing socket...HTTP/1.1 200 OK
Date: Mon, 14 Sep 2009 08:56:36 GMT
Server: Apache/2.2.3 (Red Hat)
Last-Modified: Tue, 15 Nov 2005 13:24:10 GMT
ETag: &quot;b80f4-1b6-80bfd280&quot;
Accept-Ranges: bytes
Content-Length: 438
Connection: close
Content-Type: text/html; charset=UTF-8

OK.
</pre></div>
    </div>
   </div>
  </p>
 </div>

                     
</div></div></div></body></html>